<?php
/*
ALTER TABLE `countries` ADD `image_flag` VARCHAR( 255 ) NOT NULL
 * */
require_once('common.php');
session_start();


$id = isset($_GET['id']) ? $_GET['id'] : 0;
$btnVal="Add Country";
$countryName = '';
//pr($_POST);
//////////// delete ///////////////
//country.php?mode=del&id=1
if(isset($_GET['mode']) && $_GET['mode']=='del'){
  $delQuery = "DELETE FROM `countries` WHERE `id`=$id";
  //pr($delQuery);
  mysql_query($delQuery) or die(mysql_error());
  if(mysql_affected_rows() == 1){
    pr("Country Deleted");
  }
}

//////////// edit-select///////////////
//country.php?mode=edit&id=1
if(isset($_GET['mode']) && $_GET['mode']=='edit'){
  $btnVal="Edit Country";
  $editSelectQuery = "SELECT * FROM `countries` WHERE `id`=$id";
  //pr($delQuery);
  $editSelectResult  = mysql_query($editSelectQuery) or die(mysql_error());

  $editSelectAns = mysql_fetch_assoc($editSelectResult);
  $countryName = $editSelectAns['name'];
  $countryImage = $editSelectAns['image_flag'];
//  pr($editSelectAns);
}

/// UPDATE COUNTRY
if(isset($_POST['name']) && !empty($_POST['name']) && $_POST['country_id']!=0)
{
  $id = $_POST['country_id'];
  $name = $_POST['name'];

  // upload updated image
  if($_FILES['image']['error'] == 0){

    $time_stamp = date("ymdHis");
    $fileName = $time_stamp.'_'.$_FILES['image']['name'];

    $src = $_FILES['image']['tmp_name'];
    $dest  = "flags/$fileName";
    copy($src, $dest);
    unlink("flags/".$_POST['image_flag']);
  } else {
    $fileName = $_POST['image_flag'];
  }



   $editQuery = "UPDATE `countries` SET
      `name`='$name',`image_flag`='$fileName'
   WHERE `id`=$id";

  mysql_query($editQuery) or die(mysql_error());
  if(mysql_affected_rows() == 1){
    pr("Country UPDATED");
  }
  $id =  0;
  $btnVal="Add Country";
  $countryName = '';
}
//////////// PAGING //////////////
$page_num = isset($_GET['page']) ? $_GET['page'] : 1;
$record_per_page = 3;
$limit = ($page_num-1)*$record_per_page;

#total records
$queryTotal = "SELECT COUNT(*) as `total` FROM `countries`";
$resultTotal = mysql_query($queryTotal) or die(mysql_error());
$ansTotal  = mysql_fetch_assoc($resultTotal);
$totalRecords = $ansTotal['total'];
$total = ceil($totalRecords/$record_per_page);
//////////////////////////////////
############## INSERT country ######
//pr($_POST);
if(isset($_POST['name']) && !empty($_POST['name']) && $_POST['country_id']==0)
{

  if($_SESSION['secretword'] != $_POST['captcha']) {
    echo "Invalid Security Code.";
  } else {
    if($_FILES['image']['error'] == 0){

      $time_stamp = date("ymdHis");
      $fileName = $time_stamp.'_'.$_FILES['image']['name'];

      $src = $_FILES['image']['tmp_name'];
      $dest  = "flags/$fileName";
      copy($src, $dest);
    }

    $c_name = $_POST['name'];
    $query = "INSERT INTO `countries` SET
              `image_flag` = '$fileName',
               name='$c_name'";
    mysql_query($query) or die(mysql_error());
    $last_insert_id = mysql_insert_id();
    echo "Country Inserted on ID $last_insert_id\n\n";
  }
}

## SELECT countries ######
$query = "SELECT * FROM `countries` ORDER BY `id` DESC LIMIT $limit,$record_per_page ";
$result = mysql_query($query) or die(mysql_error());
###################################
?>
<link rel="stylesheet" href="css/bootstrap.css" />
<style>
  input[type="text"]{
    font-size:20px !important;
    height:30px !important;
  }
</style>
<form name="countries" action="" method="post" enctype="multipart/form-data">

<input type="hidden" name="country_id" value="<?=$id?>">
<input type="hidden" name="image_flag"
value="<?=@$countryImage?>">

  <table class="table table-bordered">
    <tr>
      <th colspan="2"><?=$btnVal?></th>
    </tr>
    <tr>
      <td>Name : </td>
      <td><input type="text" required name="name" required value="<?=$countryName?>"></td>
    </tr>
    <tr>
      <td>Upload Flag : </td>
      <td>
<?php if($countryName) {?>
<img height="90" width="120" src="flags/<?=$countryImage?>">
<?php } ?>
        <input type="file" required name="image"  ></td>
    </tr>


    <tr>
      <td>Security Code</td>
      <td>

<!-- Captcha Start -->
<img src="captcha/captcha.php" id="captcha" /><br/>
<a href="javascript:void(0)" onclick="    document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>
<input type="text" required name="captcha" id="captcha-form" /><br/>
<!-- Captcha End-->



      </td>
    </tr>


    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" class="btn btn-success" name="submit" value="<?=$btnVal?>" ></td>
    </tr>
    </table>
</form>

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Image</th>
      <th>Options</th>
    </tr>
  <thead>

  <tbody>
<?php
  while($ans = mysql_fetch_assoc($result)){
?>
    <tr>
      <td><?=$ans['id']?></td>
      <td><?=$ans['name']?></td>
      <td><img src="flags/<?=$ans['image_flag']?>" height="90" width="120"> </td>
      <td><a href="country.php?mode=edit&id=<?=$ans['id']?>">Edit</a> / <a onClick="return confirm('Are you sure to delete?');" href="country.php?mode=del&id=<?=$ans['id']?>">Delete</a></td>
    </tr>
<?php
  } //end of while
?>

  </tbody>

</table>

<table class="table table-bordered">
  <tr>
<?php for($num=1 ; $num<=$total ; $num++){
   if($page_num == $num ){
    $td_page =  '<h4 style="color:red;font-size:30px;">'.$num.'<h4>';
   } else {
    $td_page ='<a href="country.php?page='.$num.'">'.$num.'</a>';
  }
?>
    <td><?=$td_page?></td>
<?php }?>
  </tr>
</table>
